import request from '@/common/utils/request'
import md5 from 'js-md5'

function formatSign(params) {
    var signstr = JSON.stringify(JSON.parse(params))
    var rule = '"'
    var regS = new RegExp(rule, 'g')
    var rule2 = ':'
    var regS2 = new RegExp(rule2, 'g')
    signstr = signstr.replace(regS, '')
    signstr = signstr.replace(regS2, '=')
    // eslint-disable-next-line
    signstr = signstr.replace(/\\/g, "")
    return signstr
}

// 查询下拉字典数据:[{"formColumnId":21121,"formColumnUsName":"sql13","formColumnGuiType":"sql"}]
export function getselectdatasAll(params) {
    return request({
        url: '/dict/getselectdatasAll.action',
        method: 'post',
        params
    })
}

// 列表业务数据:
// {"order":"asc","offset":0,"limit":10,"containerParam":{},"paramsMap":{},"requestParam":{"ORGVORGANIZATION_DEPCODE":""},"filter":{"ORGDEPARTMENT_DEPCODE":"","ORGDEPARTMENT_DEPGRADE":"","ORGDEPARTMENT_CNAME":""},"userIds":null,"formId":"685","isView":null,"heightGrade":[]}
export function cardformInitData(params) {
    // const params = { 'order': 'asc', 'offset': 0, 'limit': 10, 'containerParam': {}, 'paramsMap': {}, 'requestParam': { 'ORGVORGANIZATION_DEPCODE': '' }, 'filter': { 'ORGDEPARTMENT_DEPCODE': '', 'ORGDEPARTMENT_DEPGRADE': '', 'ORGDEPARTMENT_CNAME': '' }, 'userIds': null, 'formId': '685', 'isView': null, 'heightGrade': [] }
    const data = JSON.stringify(params)
    return request({
        url: '/form/cardformInitData.action',
        method: 'post',
        data
    })
}

// 用户字段权限
export function queryCardFormColumnsWithUserId(formId) {
    return request({
        url: `/form/queryCardFormColumnsWithUserId.action?formId=${formId}`,
        method: 'get'
    })
}

// 用户按钮权限
export function queryCardFormButtonsWithUserId(formId) {
    return request({
        url: `/form/queryCardFormButtonsWithUserId.action?formId=${formId}`,
        method: 'get'
    })
}

// cardformInitColumn
// 字段明细
// export function cardformInitColumn(formId) {
//     const params = { 'order': 'asc', 'offset': 0, 'limit': 10, 'containerParam': {}, 'paramsMap': {}, 'requestParam': { 'ORGVORGANIZATION_DEPCODE': '' }, 'filter': { 'ORGDEPARTMENT_DEPCODE': '', 'ORGDEPARTMENT_DEPGRADE': '', 'ORGDEPARTMENT_CNAME': '' }, 'userIds': null, 'formId': '685', 'isView': null, 'heightGrade': [] }
//     const data = JSON.stringify(params)
//     return request({
//         url: '/form/cardformInitColumn.action',
//         method: 'post',
//         data
//     })
// }

// questionModel/searchganttchartdata.action
export function searchganttchartdata(formId) {
    return request({
        url: `/questionModel/searchganttchartdata.action`,
        method: 'get'
    })
}
// questionModel/searchquestiondate.action
export function searchquestiondate(formId) {
    return request({
        url: `/questionModel/searchquestiondate.action?formId=${formId}`,
        method: 'get'
    })
}

export function getDictDatasAll(formId) {
    return request({
        url: `/dict/getDictDatasAll.action?formId=${formId}`,
        method: 'get'
    })
}

export function searchcolumdisplay(formId) {
    return request({
        url: `/colum/searchcolumdisplay.action?formid=${formId}`,
        method: 'get'
    })
}

export function savecolumdisplay(json) {
    return request({
        url: '/colum/savecolumdisplay.action',
        method: 'get',
        headers: {
            'json': JSON.stringify(json)
        }
    })
}

// /internationalization/gettranslation.action?formid=55
// 国际化
export function internation(formid) {
    return request({
        url: `/internationalization/gettranslation.action?formid=${formid}`,
        method: 'get'
    })
}

// tree
export function queryTreeSolution(solutionId, id, ORGVORGANIZATION_ID, lv) {
    let url
    if (!ORGVORGANIZATION_ID && !lv) {
        url = `/form/getTreeSolutionById.action?solutionId=${solutionId}&params={}`
    } else {
        url = `/form/getTreeSolutionById.action?solutionId=${solutionId}&params={}&${id}=${ORGVORGANIZATION_ID}&lv=${lv}`
    }

    return request({
        url: url,
        method: 'get'
    })
}

// 批量保存数据接口
export function batchSaveButtonSave(formId, params) {
    const data = JSON.stringify(params)
    return request({
        url: `/form/batchSaveButtonSave.action?formId=${formId}`,
        method: 'post',
        data
    })
}

// /form/removeData.action?sign=d2c9a1a835aa13fee8d81138a90689b7
// 表单数据删除
export function removeData(params) {
    const data = JSON.stringify(params)
    const sign = md5(formatSign(data))
    return request({
        url: `/form/removeData2.action?sign=${sign}`,
        method: 'post',
        data
    })
}

// /POST /form/saveButtonSave.action?sign=556d3c3eb345cceb47834de84471fb0e 表单数据增加
// 表单数据增加
export function saveButtonSave(params) {
    const data = JSON.stringify(params)
    const sign = md5(formatSign(data))
    return request({
        url: `/form/saveButtonSave.action?sign=${sign}`,
        method: 'post',
        data
    })
}

export function findFormList(formParam) {
    return request({
        url: `/form/findFormList.action?sort=form_def_id&order=asc&offset=0&limit=10&condition=${formParam}&pageSize=1&folderTreeCode=0&_=1551792&swaggerUserCode=sysadmin`,
        method: 'get'
    })
}

export function queryFormLayOutDetail(formId) {
    return request({
        url: `/formlayout/queryFormLayOutDetail.action?formid=${formId}&swaggerUserCode=sysadmin`,
        method: 'get'
    })
}

export function updateFormLayOut(formId, templateList) {
    const data = JSON.stringify(templateList)
    return request({
        url: `/formlayout/updateFormLayOut.action?formid=${formId}&folder_id=111&swaggerUserCode=sysadmin`,
        method: 'post',
        data
    })
}

// 获取图表表单数据
export function getEchartsDatas(formId) {
    return request({
        url: `/echarts/getEchartsDatas.action?formId=${formId}`,
        method: 'get'
    })
}

// 获取下载表单数据
export function getFormField(formId) {
    return request({
        url: `/form/getFormField.action?formId=${formId}`,
        method: 'post'
    })
}

// /myehr/form/getImportFileHead.action?fileid='+fileid
export function getImportFileHead(fileid) {
    return request({
        url: `/form/getImportFileHead.action?fileid=${fileid}`,
        method: 'post'
    })
}

// /myehr/form/importExcelData.action?importType=old
export function importExcelData(importType, params) {
    const data = JSON.stringify(params)
    return request({
        url: `/form/importExcelData.action?importType=${importType}`,
        method: 'post',
        data
    })
}

// '/myehr/record/recording.action?formid='+formId,
export function recording(formid) {
    return request({
        url: `/form/record/recording.action?formid=${formid}`,
        method: 'post'
    })
}

// /announcement/exportS.action
export function exportField(filterdata) {
    const data = JSON.stringify(filterdata)
    return request({
        url: `/announcement/exportS.action`,
        method: 'post',
        data
    })
}

// /myehr/form/getExcSolutionByFormId.action?formButtonId='+formButtonId+'&formId='+formId
export function getExcSolutionByFormId(formButtonId, formId) {
    return request({
        url: `/form/getExcSolutionByFormId.action?formButtonId=${formButtonId}&formId=${formId}`,
        method: 'post'
    })
}

// /myehr/form/getSysRuleBySolutionId.action?solutionId='+solutionId
export function getSysRuleBySolutionId(solutionId) {
    return request({
        url: `/form/getSysRuleBySolutionId.action?solutionId=${solutionId}`,
        method: 'post'
    })
}

// /myehr/form/checkAndExcSolutionParams.action?ruleIds='+ruleIds+'&formId='+formId+'&buttonId='+buttonId
export function checkAndExcSolutionParams(ruleIds, formId, buttonId) {
    return request({
        url: `/form/checkAndExcSolutionParams.action?ruleIds=${ruleIds}&formId=${formId}&buttonId=${buttonId}`,
        method: 'post'
    })
}

// /myehr/form/excRule.action
export function excRule(ruleIds, formId, buttonId) {
    const data = { ruleIds, formId, buttonId }
    return request({
        url: `/form/excRule.action`,
        method: 'post',
        data: JSON.stringify(data)
    })
}

// /myehr/form/exportCardformInitData.action
export function exportCardformInitData(data) {
    return request({
        url: `/form/exportCardformInitData.action`,
        method: 'post',
        data: JSON.stringify(data)
    })
}

// 表单数据删除
export function removeDataByButton(params) {
    const data = JSON.stringify(params)
    const sign = md5(formatSign(data))
    return request({
        url: `/form/removeData.action?sign=${sign}`,
        method: 'post',
        data: data
    })
}

// http://119.254.89.122:9882/myehr/CardForm/SaveCardForm.action?swaggerUserCode=sysadmin
export function SaveCardForm(data) {
    return request({
        url: `/CardForm/SaveCardForm.action?formid=${data.config.formId}&swaggerUserCode=sysadmin`,
        method: 'post',
        data: JSON.stringify(data)
    })
}

// /myehr/CardForm/searchCardForm.action?formid=XXX&swaggerUserCode=sysadmin
export function searchCardForm(formid) {
    return request({
        url: `/CardForm/searchCardForm.action?formid=${formid}&swaggerUserCode=sysadmin`,
        method: 'post'
    })
}
